package com.alumni_circle.core.web.rbac;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.access.AccessLog;
import com.alumni_circle.core.entity.rbac.Authority;
import com.alumni_circle.core.entity.rbac.Role;
import com.alumni_circle.core.entity.rbac.VO.AuthorityInsertVO;
import com.alumni_circle.core.entity.rbac.VO.AuthoritySearchVO;
import com.alumni_circle.core.entity.rbac.VO.AuthorityUpdateVO;
import com.alumni_circle.core.entity.rbac.VO.AuthorityViewVO;
import com.alumni_circle.core.service.rbac.IAuthorityService;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.utils.ToLowerCaseFirstOneUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.me.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 龙小虬
 * @since 2020-08-02 20:43
 */
@Api(tags = "权限管理")
@Controller
@RequestMapping("/authority")
public class AuthorityController extends BaseController<Authority> {

    @Resource
    IAuthorityService authorityService;

    @Resource
    private RequestMappingHandlerMapping rMhm;

    @Resource
    ToLowerCaseFirstOneUtils toLowerCaseFirstOneUtils;

    /**
     * 增加权限信息
     * @param authorityInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加权限信息", notes = "增加权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="增加权限信息",url="/authority/addAuthority")
    public ResponseModel addAuthority(@ApiParam(required = true, name = "user")
                                 @RequestBody AuthorityInsertVO authorityInsertVO) {
        Authority authority = ModelUtils.transform(authorityInsertVO,Authority.class);
        User user = SecurityUtils.getUser();
        authority.setCreateBy(user.getId());
        authority.setCreateName(user.getName());
        authorityService.save(authority);
        return new ResponseModel<>(authority.getId());
    }

    /**
     * 查询权限信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询权限信息", notes = "查询权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询权限信息",url="/authority/selectAuthority")
    public ResponseModel selectAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                             @RequestBody BaseId baseId) {
        return new ResponseModel<>();
    }

    /**
     * 搜索权限信息
     * @param authoritySearchVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询权限信息", notes = "查询权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询权限信息",url="/authority/searchAuthority")
    public ResponseListModel searchAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                             @RequestBody AuthoritySearchVO authoritySearchVO) {
        return new ResponseListModel<>(authorityService.searchAuthority(authoritySearchVO.getId(), authoritySearchVO.getComment(), authoritySearchVO.getState(), authoritySearchVO.getUpdateName(), authoritySearchVO.getUpdateTime(), authoritySearchVO.getUrl()));
    }

    /**
     * 搜索权限信息
     * @param authoritySearchVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchListAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询权限信息", notes = "查询权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询权限信息",url="/authority/searchAuthority")
    public ResponseListModel searchListAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                             @RequestBody AuthoritySearchVO authoritySearchVO) {
        PageHelper.startPage(authoritySearchVO.getPageNum(),authoritySearchVO.getPageSize());
        List<Authority> list = authorityService.list(new QueryWrapper<Authority>()
                .like("permissions",authoritySearchVO.getPermissions())
                .like("url",authoritySearchVO.getUrl()));
        PageInfo<Authority> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(ModelUtils.copyListProperties(list,AuthorityViewVO.class),String.valueOf(pageInfo.getTotal()));
    }


    /**
     * 删除权限信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除权限信息",url="/authority/deleteAuthority")
    public ResponseModel deleteAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                    @RequestBody BaseId baseId) {
        authorityService.removeById(baseId.getId());
        return new ResponseModel<>(baseId.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/disAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "禁用权限信息", notes = "禁用权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="禁用权限信息",url="/authority/disAuthority")
    public ResponseModel disAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                         @RequestBody BaseId baseId) {
        Authority authority = new Authority();
        authority.setId(baseId.getId());
        authority.setState("1");
        authorityService.updateById(authority);
        return new ResponseModel<>(baseId.getId());
    }

    /**
     * 更新权限信息
     * @param authorityUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新权限信息", notes = "更新权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新权限信息",url="/authority/updateAuthority")
    public ResponseModel updateAuthority(@ApiParam(required = true, name = "UserInsertVO")
                                    @RequestBody AuthorityUpdateVO authorityUpdateVO) {
        Authority authority = ModelUtils.transform(authorityUpdateVO,Authority.class);
        User user = SecurityUtils.getUser();
        authority.setUpdateName(user.getName());
        authority.setUpdateBy(user.getId());
        authorityService.updateById(authority);
        return new ResponseModel<>(authority.getId());
    }


    /**
     * 查询所有权限信息，可分页查询
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询权限信息", notes = "查询符合条件权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询权限信息",url="/authority/getAllAuthority")
    public ResponseListModel getAllAuthority(@ApiParam(required = true, name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        //分页查询
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        System.out.println(pageBase);
        List<AuthorityViewVO> list = authorityService.getAll();
        System.out.println(list);
        PageInfo<AuthorityViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 查询所有权限信息，可分页查询
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllPermissionsUrl", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有权限名+url", notes = "查询所有权限名+url", httpMethod = "GET", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有权限名+url",url="/authority/getAllPermissionsUrl")
    public ResponseListModel getAllAuthority() {
        return new ResponseListModel<>(authorityService.getAllPermissionsUrl());
    }


    @ResponseBody
    @RequestMapping(value = "/getAllUrl", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取所有url", notes = "获取所有url", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="获取所有url",url="/authority/getAllUrl")
    public ResponseModel getAllUrl() {
        String urls = "";
        Map<RequestMappingInfo, HandlerMethod> handlermethod= rMhm.getHandlerMethods();
        Collection< HandlerMethod> methods = handlermethod.values();
        for (HandlerMethod method : methods) {
            if(method.toString().contains("com.alumni_circle")) {
                String url = method.toString();
                //com.educational_system.controller.AssessStudentController#addAssessStudent(AssessStudent)
                url = url.substring(url.lastIndexOf(".") + 1);
                //AssessStudentController#addAssessStudent(AssessStudent)
                url = toLowerCaseFirstOneUtils.toLower(url);
                //assessStudentController#addAssessStudent(AssessStudent)
                url = url.substring(0, url.lastIndexOf("("));
                //assessStudentController#addAssessStudent
                url = url.replace("#", "/");
                //assessStudentController/addAssessStudent
                url = url.replace("Controller","");
                url = "/"+url+",";
                urls += url;
                System.out.println(urls);
            }
        }
        return new ResponseModel(urls);
    }
}
